package App.Job;

import App.Model.Task.TaskJobInfo;
import App.Repository.JobRepository;
import App.Repository.XinGePushRepository;
import App.Service.TaskService;
import App.Service.XingeService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by xw on 2019/1/4.
 */

public class MyJob implements Job, Serializable {


    //信鸽推送服务
    private XingeService _xingeService = new XingeService();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {


        String _title = "";
        String _content = "";
        List<String> _userids = new ArrayList<>();

        try {

            logger.info("begin task job");
            JobDetail detail = context.getJobDetail();
            String _reminderid = detail.getJobDataMap().getString("reminderid");
            String _taskid = detail.getJobDataMap().getString("taskid");
            String _taskContent = detail.getJobDataMap().getString("taskcontent");
            String _taskAddress = detail.getJobDataMap().getString("taskaddress");
            String taskBegintime = detail.getJobDataMap().getString("taskbegintime");


            SimpleDateFormat _dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date _strToDate = _dataFormat.parse(taskBegintime);
            taskBegintime = _dataFormat.format(_strToDate);


            //推送消息
            //提                                               

            //可以拿到提醒的id
            //动态计算提醒的接收人

            //推送任务提醒消息

            // TaskJobInfo _taskInfo = _taskService.getReminderMember(_reminderid);

            JobRepository _jobRepository = new JobRepository();
            TaskJobInfo _taskInfo = _jobRepository.getJobData(_reminderid, _taskid);
            for (Integer id : _taskInfo.getMembers()) {
                String _id = id.toString();
                if (!_userids.contains(_id)) {
                    _userids.add(id.toString());
                }
            }
            _content = _taskInfo.getTaskcontent();
            String _beginTime = _taskInfo.getTaskbegintime();
            String _addressInfo = _taskInfo.getAddress();

            String _finalString = _taskContent + "                                                              ";
            _finalString += "时间:" + taskBegintime + "                                                        ";
            _finalString += "地点:" + _taskAddress;

            _title = _taskContent;
            _finalString = "时间:" + taskBegintime;

            //   logger.info("task job message:" + _finalString);
            _xingeService.pushMessage(_taskid, _reminderid, _userids, _title, _finalString);


            logger.info("end task job");


        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info("send job messge error:" + _content);
            logger.info("error:" + ex.getMessage());
        }
    }
}
